using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

namespace DL
{
    [System.Obsolete("此类已被弃用，请使用AudioManager类", true)]//标记该方法已弃用
    public class AudioManager_old : Singleton<AudioManager_old>
    {
        public List<SoundData> Sounds;
        //每一个音频名称对应一个音频组件
        private Dictionary<string, AudioSource> m_AudioDict;
        
        protected override void Construct()
        {
            m_AudioDict = new Dictionary<string, AudioSource>();
            foreach (var sound in Sounds)
            {
                var go = new GameObject(sound.clip.name, typeof(AudioSource));
                go.transform.SetParent(transform);
                
                var source = go.GetComponent<AudioSource>();
                source.clip = sound.clip;
                // source.playOnAwake = sound.playOnAwake;
                source.loop = sound.loop;
                // source.volume = sound.volume;
                source.outputAudioMixerGroup = sound.outputGroup;
                // if (sound.playOnAwake) source.Play();
                m_AudioDict.Add(sound.clip.name, source);
            }
        }

        protected override void Release()
        {
            
        }

        /// <summary>
        /// 播放方法
        /// </summary>
        /// <param name="clipName">clipName</param>
        /// <param name="isWait">正在播放该剪辑是否跳过播放</param>
        public void PlayAudio(string clipName,bool isWait = false)
        {
            if (!m_AudioDict.TryGetValue(clipName, out var source))
            {
                Debug.LogWarning("音频不存在");
                return;
            }

            if (isWait)
            {
                if (source.isPlaying)
                {
                    return;
                }
            }
            source.Play();
        }

        /// <summary>
        /// 停止播放方法
        /// </summary>
        /// <param name="clipName">clipName</param>
        public void StopAudio(string clipName)
        {
            if (!m_AudioDict.TryGetValue(clipName, out var source))
            {
                Debug.LogWarning("音频不存在");
                return;
            }
            
            source.Stop();
        }

        /// <summary>
        /// 音频混响器
        /// </summary>
        [SerializeField,Header("音频混响器")]
        private AudioMixer audioMixer;
        
        /// <summary>
        /// 设置背景音乐声音
        /// </summary>
        /// <param name="value"></param>
        public void SetBGMVolume(float value)
        {
            audioMixer.SetFloat("BGM", value);
        }

        /// <summary>
        /// 设置音效声音
        /// </summary>
        /// <param name="value"></param>
        public void SetSFXVolume(float value)
        {
            audioMixer.SetFloat("SFX", value);
        }
        
    }
}

